package edu.jd.software.web.backend.controller;

import edu.jd.software.web.backend.common.api.CommonResult;
import edu.jd.software.web.backend.mongodb.document.MemberReadHistory;
import edu.jd.software.web.backend.service.MemberReadHistoryService;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Controller
@Tag(name = "MemberReadHistoryController", description = "会员商品浏览记录管理")
@RequestMapping("/member/readHistory")
public class MemberReadHistoryController {
    @Autowired
    private MemberReadHistoryService memberReadHistoryService;

    @Operation(description = "创建浏览记录")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult create(@RequestBody MemberReadHistory memberReadHistory) {
        int count = memberReadHistoryService.create(memberReadHistory);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    @Operation(description = "根据文档的ID删除浏览记录")
    @RequestMapping(value = "/deleteById", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult deleteById(@RequestParam("ids") List<String> ids) {
        int count = memberReadHistoryService.deleteById(ids);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    @Operation(description = "展示浏览记录")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<List<MemberReadHistory>> list(Long memberId) {
        List<MemberReadHistory> memberReadHistoryList = memberReadHistoryService.list(memberId);
        return CommonResult.success(memberReadHistoryList);
    }

    @Operation(description = "根据会员ID批量删除浏览记录-衍生方法")
    @RequestMapping(value = "/deleteByMemId1", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult deleteByMemId1(@RequestParam("ids") List<Long> ids) {
        int count = memberReadHistoryService.deleteByMemId1(ids);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }
    @Operation(description = "根据会员ID批量删除浏览记录-利用衍生查询查出后的结果进行删除")
    @RequestMapping(value = "/deleteByMemId2", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult deleteByMemId2(@RequestParam("ids") List<Long> ids) {
        int count = memberReadHistoryService.deleteByMemId2(ids);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }
    @Operation(description = "根据会员ID批量删除浏览记录-通过注解实现")
    @RequestMapping(value = "/deleteByMemId3", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult deleteByMemId3(@RequestParam("ids") List<Long> ids) {
        int count = memberReadHistoryService.deleteByMemId3(ids);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }
}
